<?php

namespace iflow\swoole\implement\Client\Rpc;

use iflow\Container\Container;
use iflow\Container\implement\generate\exceptions\InvokeClassException;
use iflow\swoole\implement\Client\Rpc\Services\Subscription;
use Swoole\Process;
use function Co\run;

class Service extends \iflow\swoole\implement\Server\Rpc\Service {

    protected Subscription $subscription;

    protected function createServiceAfter(): void {
        parent::createServiceAfter(); // TODO: Change the autogenerated stub

        // 注册服务
        $subscription = new Process(function () {
            swoole_set_process_name(uniqid('iflow_rpc_client_'. $this->getClientNames()));
            run(function () {
                $this->subscription = $this->getSubscription();
                $this->subscription -> subscription();
            });
        });
        $this->SwService -> addProcess($subscription);
    }


    /**
     * 获取订阅服务
     * @return Subscription
     * @throws InvokeClassException
     */
    protected function getSubscription(): Subscription {
        return app(Subscription::class, [ $this->config -> get('server'), $this ]);
    }

    public function getClientNames() {
        return $this->config -> get('client_name', '');
    }

}
